using System.Reflection;
using Send2Sharepoint.Common;
using Send2Sharepoint.Forms;

namespace Send2Sharepoint
{
	using System;
	using Extensibility;
	using System.Runtime.InteropServices;

	#region Read me for Add-in installation and setup information.
	// When run, the Add-in wizard prepared the registry for the Add-in.
	// At a later time, if the Add-in becomes unavailable for reasons such as:
	//   1) You moved this project to a computer other than which is was originally created on.
	//   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
	//   3) Registry corruption.
	// you will need to re-register the Add-in by building the Send2SharepointSetup project, 
	// right click the project in the Solution Explorer, then choose install.
	#endregion
	
	/// <summary>
	///   The object for implementing an Add-in.
	/// </summary>
	/// <seealso class='IDTExtensibility2' />
	[GuidAttribute("AEA395BA-1190-4157-8F94-99A63DB98B94"), ProgId("Send2Sharepoint.Connect")]
	public partial class Connect : Object, IDTExtensibility2
	{
        private static IndexDocument _indexDocument;
		public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
		{
		    Globals.ThisApplication = application;
		    Globals.ThisInstance = addInInst;
            if (connectMode != ext_ConnectMode.ext_cm_Startup)
                OnStartupComplete(ref custom);
		}
		
		public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
		{
            if (disconnectMode != ext_DisconnectMode.ext_dm_HostShutdown)
            {
                OnBeginShutdown(ref custom);
            }
		    Globals.ThisApplication = null;
		}

		public void OnAddInsUpdate(ref Array custom)
		{
		}

		public void OnStartupComplete(ref Array custom)
		{
            Globals.ConfigFile = Assembly.GetExecutingAssembly().Location + ".config";
            Globals.UserName = CustomConfigurationSettings.AppSettings(Globals.ConfigFile)["UserName"].ToString();
            Globals.Password = CustomConfigurationSettings.AppSettings(Globals.ConfigFile)["Password"].ToString();
            Globals.DocumentLibrary = CustomConfigurationSettings.AppSettings(Globals.ConfigFile)["DocumentLibrary"].ToString();
		    Globals.SharepointSiteUrl = CustomConfigurationSettings.AppSettings(Globals.ConfigFile)["SharepointSiteUrl"].ToString();
            if (Globals.ThisApplication is Microsoft.Office.Interop.Outlook.Application)
                InitializeOutlook();
            else if (Globals.ThisApplication is Microsoft.Office.Interop.Word._Application)
                InitializeWord();
            else if (Globals.ThisApplication is Microsoft.Office.Interop.Excel._Application)
                InitializeExcel();
            Logging.WriteToLog("Start up has completed.");
		}

		public void OnBeginShutdown(ref Array custom)
		{
            if (Globals.ThisApplication is Microsoft.Office.Interop.Outlook.Application)
                RemoveOutlook();
            else if (Globals.ThisApplication is Microsoft.Office.Interop.Word._Application)
                RemoveWord();
            else if (Globals.ThisApplication is Microsoft.Office.Interop.Excel._Application)
                RemoveExcel();
		}
	}
}